package cn.ac.iie.di.ban.data.exchange.runner.slave;

import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.ScopeEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.StatusEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.TaskTypeEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskOrderRequest;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskOrderResponse;
import cn.ac.iie.di.ban.data.exchange.runner.commons.worker.SlaveWorker;
import cn.ac.iie.di.ban.data.exchange.runner.slave.task.checkTask.CheckSQLAndPermissionTask;
import cn.ac.iie.di.ban.data.exchange.runner.slave.task.runTask.DownloadTask;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by Black-Laptop on 12/6/2017.
 */
public class DownloadSlave implements SlaveWorker {

    public static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DownloadSlave.class);

    private TaskTypeEnum type;
    private ScopeEnum scope;

    private DownloadTask downloadTask;

    private TaskOrderRequest taskOrderRequest;
    private String uniq_key;

    private String MMUrls;
    private String MMnameBase;

    public static FinishedHook callAfterFinished;

    private String ftpIp;
    private String ftpUser;
    private String ftpPassword;
    private int ftpPort;

    /**
     * TaskTypeEnum.CHECK 功能时不需要传任何参数
     */
    public DownloadSlave() {
    }

    /**
     * 调用媒体库
     *
     * @param MMUrls
     * @param MMnameBase
     */
    public DownloadSlave(String MMUrls, String MMnameBase) {
        LOG.info("DownloadSlave  " + "Urls:" + MMUrls + "  nameBase:" + MMnameBase);
        this.MMUrls = MMUrls;
        this.MMnameBase = MMnameBase;

        this.ftpIp = ftpIp;
        this.ftpPort = ftpPort;
        this.ftpUser = ftpUser;
        this.ftpPassword = ftpPassword;
    }


    @Override
    public String getName() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public TaskOrderResponse execute(TaskOrderRequest taskOrderRequest, FinishedHook callAfterFinished) {
        this.type = taskOrderRequest.getType();
        this.scope = taskOrderRequest.getScope();
        DownloadSlave.callAfterFinished = callAfterFinished;

        this.taskOrderRequest = taskOrderRequest;
        this.uniq_key = taskOrderRequest.getUniq_key();

        LOG.info("excute  " + "type:" + type + "  scope:" + scope + "  uniq_key:" + uniq_key);

        TaskOrderResponse taskOrderResponse = null;

        try {
            taskOrderResponse = start();
            return taskOrderResponse;
        } catch (Exception ex) {
            taskOrderResponse = new TaskOrderResponse(uniq_key, StatusEnum.DOWNLOAD_TASK_ERROR, ex.getMessage());
        }

        return taskOrderResponse;

    }

    @Override
    public boolean finished() {
        return getDownloadTaskThreadStatus();
    }

    @Override
    public void stop() {
        stopDownloadTaskThread();
    }

    private TaskOrderResponse start() throws Exception {

        TaskOrderResponse taskOrderResponse = null;

        switch (this.type) {
            case CHECK: {
                CheckSQLAndPermissionTask checkSQLAndPermissionTask = new CheckSQLAndPermissionTask(this.taskOrderRequest);
                taskOrderResponse = checkSQLAndPermissionTask.checkSQLAndPermission(uniq_key);
                break;
            }
            case RUN: {
                taskOrderResponse = runTask();
                break;
            }
            default:
                break;
        }

        return taskOrderResponse;
    }

    private Boolean getDownloadTaskThreadStatus() {
        if (this.downloadTask != null) {
            return downloadTask.getDownloadTaskThreadStatus();
        }

        return false;
    }

    private void stopDownloadTaskThread() {
        if (this.downloadTask != null) {
            downloadTask.stopDownloadTaskThread();
        }
    }

    private TaskOrderResponse runTask() throws Exception {
        CheckSQLAndPermissionTask checkSQLAndPermissionTask = new CheckSQLAndPermissionTask(this.taskOrderRequest);
        checkSQLAndPermissionTask.checkSQLAndPermission(uniq_key);

        TaskOrderResponse taskOrderResponse = null;

        Boolean doDownload = ScopeEnum.MEDIA.equals(scope);
        if (doDownload) {
            this.downloadTask = new DownloadTask(this.taskOrderRequest, MMUrls, MMnameBase);


            taskOrderResponse = downloadTask.doDownloadTask();
            return taskOrderResponse;
        }

        return taskOrderResponse;
    }


}
